import Calendar from './calendar.js'  // 日期相关计算方法对象
import BaziInfo from './data.js'  // 八字相关基础信息对象
import AreaInfo from './areaInfo.js';  // 地区信息
import ApparentSolarTime from './apparentSolarTime.js';  // 每日真太阳时与平太阳时之差值表（用平太阳时加减差值得真太阳时时）

/**
 * 首页八字测算方法
 *  @param {Object} param 用户填写信息
 *  @eg {
 *      name: '',  // 姓名
 *      sex: 0,  // 性别
 *      place: '一级-二级',  // 地区
 *      birthday: 'YYYY-MM-DD HH:mm',  // 用户选择时间
 *      isSolar: 0,  // 是否选择真太阳时 0否 1是
 *      ...传入的其他参数
 *  }
 *  @return {Object} 返回计算后的信息
 */
export function baziCalc(param) {
  // 名称,性别,地区
  let name = param.name || '';
  let sex = param.sex || 0;
  let place = param.place || '';
  let birthday = param.birthday;
  let isSolar = param.isSolar;
  // 转换真太阳时
  let solartime = param.birthday;
  if (param.isSolar == '1') {
    solartime = getSolartime(birthday, place)['solartime'];
  }


  // 年,月,日,时
  let tempDate = solartime.split(" ");;
  let [dateArr, timeArr] = [tempDate[0].split("-"), tempDate[1].split(":")];
  let year = parseInt(dateArr[0]);  // y
  let month = parseInt(dateArr[1]);  // z
  let day = parseInt(dateArr[2]);  // G
  let hour = parseInt(timeArr[0]);  // Z
  let minute = parseInt(timeArr[1]);  // b
  param.year = year;
  param.month = month;
  param.day = day;
  param.hour = hour;
  param.minute = minute;

  // 八字-转农历日期后的当天详细信息
  let lunarDateInfo = Calendar.solar2lunar(year, month, day, hour, minute)
  // 阳历转换农历,农历23点为变换点
  if (23 == hour) {
    if (day < Calendar.solarDays(year, month)) {
      // 过23点且当月天数足够,天数+1
      lunarDateInfo = Calendar.solar2lunar(year, month, parseInt(day) + 1, hour, minute);
    } else if (month < 12) {
      // 天数不够,月数足够,月数+1,天数变为1
      Calendar.solarDays(year, parseInt(month) + 1);
      lunarDateInfo = Calendar.solar2lunar(year, parseInt(month) + 1, 1, hour, minute);
    } else {
      // 天数不够,月数不够,年数+1,天数,月数变为1
      lunarDateInfo = Calendar.solar2lunar(parseInt(year) + 1, 1, 1, hour, minute);
    }
  }
  // 八字-生肖拼音
  let shengxiaoIndex = BaziInfo.shiershegnxiao.findIndex(ele => ele == lunarDateInfo.Animal);
  let shiershegnxiaoPinYin = BaziInfo.shiershegnxiaoPinYin[shengxiaoIndex];
  // 八字-四柱拼音
  let sizhupinyin = getBazi(lunarDateInfo, hour);
  // 八字-四柱中文
  let baziChName = getBaziChNames(sizhupinyin);
  // 八字-地势
  let dishi = getDishi(sizhupinyin);
  // 八字-自坐(星运)
  let zizuo = getZizuo(sizhupinyin);
  // 八字-纳音
  let nayin = getNayin(sizhupinyin);
  // 八字-藏干中文
  let cangganChNames = getCangganChNames(sizhupinyin);
  // 八字-神煞
  let shensha = getShensha(sizhupinyin);
  // 八字-胎元
  let taiyuan = getTaiyuan(sizhupinyin);
  // 八字-命宫
  let minggong = getMinggong(sizhupinyin);
  // 八字-对冲生肖
  let duichongshegnxiao = getDuichong(sizhupinyin);
  // 八字-三煞位
  let sanshawei = getSansha(sizhupinyin);
  // 八字-文昌位
  let wenchangwei = getWenchangwei(sizhupinyin);
  // 八字-本命佛
  let benmingfo = getBenmingfo(lunarDateInfo);
  // 八字-十神
  let baziShishen = getBaziShishen(sizhupinyin, sex);
  // 八字-藏干十神
  let cangganShishen = getCangganShishen(sizhupinyin);
  // 八字-十年大运
  let shiniandayun = getShiniandayun(sizhupinyin, lunarDateInfo, hour, minute, sex);
  // 八字-空亡
  let kongwang = getKongwang(sizhupinyin);
  // 八字-神煞中补充空亡属性
  addShenshaKongwang(baziChName, kongwang, shensha);
  // 八字-流年详细内容
  let liunianDetail = getLiunianData(shiniandayun.dayun[0].nianfen, sizhupinyin);
  // 八字-福财喜贵
  let fucaixigui = getFucaixigui(sizhupinyin);
  return {
    param,
    lunarDateInfo,
    shiershegnxiaoPinYin,
    name,
    sex,
    place,
    birthday,
    isSolar,
    solartime,
    sizhupinyin,
    baziChName,
    dishi,
    zizuo,
    nayin,
    cangganChNames,
    shensha,
    taiyuan,
    minggong,
    duichongshegnxiao,
    sanshawei,
    wenchangwei,
    benmingfo,
    baziShishen,
    cangganShishen,
    shiniandayun,
    kongwang,
    liunianDetail, // 流年详细内容,首次计算对应为十年大运第一年内容
    fucaixigui
  }
}
/**
 * 流年详细内容,此方法为用户切换流年大运年份时对应计算出的数据
 *  @param {Object} year  年份
 *  @param {Object} sizhupinyin  八字四柱的拼音
 */
export function getLiunianData(year, sizhupinyin) {
  let dayunliunianArr = [];
  for (let i = year; i < year + 10; i++) {
    if (i > 2100) {
      dayunliunianArr.push({
        year: '...',
        ganzhi: '...',
        ganzhiArr: ['.', '.'],
        shishen: '.',
        cangganshishen: '.'
      })
    } else {
      let info = Calendar.solar2lunar(i, 6, 6, 6, 6);
      let bazi = getBazi(info, 6);
      let shishen = get1TianganShishen(sizhupinyin.ri.tiangan, bazi.nian.tiangan);
      let liunianCanggan = BaziInfo.dizhi[bazi.nian.dizhi].canggan;
      let cangganshishen = get1TianganShishen(sizhupinyin.ri.tiangan, liunianCanggan[0]);
      dayunliunianArr.push({
        year: i,
        ganzhi: info.gzYear,
        ganzhiArr: [...info.gzYear],
        shishen: shishen,
        cangganshishen: cangganshishen
      })
    }
  };
  return dayunliunianArr
}







/**
 * 根据日期和地区转换真太阳时计算方法
 *  @param {String} date 日期(公历) YYYY-MM-DD HH:mm
 *  @param {String} area 地区 一级-二级
 */
export function getSolartime(date, area) {
  let firstArea = area.split("-")[0];
  let secondArea = area.split("-")[1];
  let solartime = date;
  let lat = 0;  // 纬度
  let lng = 0;  // 经度

  // 一级地区不是其他国家/地区时,需计算真太阳时
  if ("其他国家/地区" != firstArea) {
    for (let item of AreaInfo[firstArea]) {
      if (item.name == secondArea) {
        lat = item.lat;
        lng = item.lng;
        break
      }
    }
    // 根据经度差转换时间为真太阳时
    solartime = getLngTime(date, lng);
    return { solartime, lat, lng }
  }
  return { solartime, lat, lng }
}


/**
 * 根据经度差转换时间
 *  @param {String} date 日期(公历) YYYY-MM-DD HH:mm
 *  @param {String} lng 经度
 */
function getLngTime(date, lng) {
  let l = date.split(" ");  // l = ['YYYY-MM-DD', 'HH:mm']
  let t = l[0].split("-");  // t = ['YYYY', 'MM', 'DD']
  l = l[1].split(":");  // l = ['HH', 'mm']
  parseInt(t[0]);
  // 月
  let g = parseInt(t[1]);  // MM
  // 日
  let m = parseInt(t[2]);  // DD
  parseInt(l[0]), parseInt(l[1]);
  // e:差值
  let e = ApparentSolarTime[g + "-" + m];
  // 经度差
  let s = lng - 120;
  let i = Math.trunc(238 * s + e);
  date = (date = date.substring(0, 19)).replace(/-/g, "/");
  var r = new Date(date).getTime() + 1e3 * i;
  let o = new Date(r);

  return function (date) {
    let n = new Date(date);
    let l = n.getFullYear();
    let t = n.getMonth() + 1;
    let e = n.getDate();
    let g = n.getHours();
    let m = n.getMinutes();
    n.getSeconds();
    return l + "-" + (t < 10 ? "0" + t : t) + "-" + (e < 10 ? "0" + e : e) + " " + (g <
      10 ? "0" + g : g) + ":" + (m < 10 ? "0" + m : m);
  }(o);
}
/**
 * 获取八字拼音方法
 *  @param {Object} a 农历日期的当天详细信息
 *  @param {String} i 小时
 */
function getBazi(a, i) {
  var h = {};
  h.nian = {}, h.yue = {}, h.ri = {}, h.shi = {}, h.nian.tiangan = getTianganPinyin(a.gzYear[0]),
    h.yue.tiangan = getTianganPinyin(a.gzMonth[0]), h.ri.tiangan = getTianganPinyin(a.gzDay[0]),
    h.nian.dizhi = getDizhiPinyin(a.gzYear[1]), h.yue.dizhi = getDizhiPinyin(a.gzMonth[1]),
    h.ri.dizhi = getDizhiPinyin(a.gzDay[1]);
  var e = (i / 2).toFixed(0) % 12;
  return h.shi.tiangan = BaziInfo.tianganNames[(BaziInfo.tianganNames.indexOf(BaziInfo.tiangan[h.ri.tiangan].rishangqishi) + e) % 10],
    h.shi.dizhi = BaziInfo.dizhiNames[e], h;
}
/**
 * 获取天干对应的拼音
 *  @param {String} a 天干名称
 */
function getTianganPinyin(a) {
  for (var i in BaziInfo.tiangan) {
    if (BaziInfo.tiangan[i].name == a) return i;
  }
}
/**
 * 获取地支对应的拼音
 *  @param {String} a 地支名称
 */
function getDizhiPinyin(a) {
  for (var i in BaziInfo.dizhi) {
    if (BaziInfo.dizhi[i].name == a) return i;
  }
}
/**
 * 获取地支对应的拼音
 *  @param {Object} a  八字四柱的拼音
 */
function getBaziChNames(a) {
  var i = {};
  return i.nian = {}, i.yue = {}, i.ri = {}, i.shi = {}, i.nian.tiangan = BaziInfo.tiangan[a.nian.tiangan].name,
    i.yue.tiangan = BaziInfo.tiangan[a.yue.tiangan].name, i.ri.tiangan = BaziInfo.tiangan[a.ri.tiangan].name,
    i.shi.tiangan = BaziInfo.tiangan[a.shi.tiangan].name, i.nian.dizhi = BaziInfo.dizhi[a.nian.dizhi].name,
    i.yue.dizhi = BaziInfo.dizhi[a.yue.dizhi].name, i.ri.dizhi = BaziInfo.dizhi[a.ri.dizhi].name,
    i.shi.dizhi = BaziInfo.dizhi[a.shi.dizhi].name, i;
}
/**
 * 获取八字地势信息
 *  @param {Object} a  八字四柱的拼音
 */
function getDishi(a) {
  var i = a.ri.tiangan, h = {};
  return h.nian = getDishiWithDizhi(i, a.nian.dizhi), h.yue = getDishiWithDizhi(i, a.yue.dizhi),
    h.ri = getDishiWithDizhi(i, a.ri.dizhi), h.shi = getDishiWithDizhi(i, a.shi.dizhi),
    h;
}
/**
 * 根据八字地支获取地势信息
 *  @param {String} a  天干
 *  @param {String} i  地支
 */
function getDishiWithDizhi(a, i) {
  var h = BaziInfo.dizhiNames.indexOf(i), e = BaziInfo.dizhiNames.indexOf(BaziInfo.tiangan[a].cangshenDizhi), s = BaziInfo.tiangan[a].yinyang, n = (12 + Math.pow(-1, s) * h - Math.pow(-1, s) * e) % 12;
  return BaziInfo.shierchangshegnNames[n];
}
/**
 * 获取八字自坐(星运)信息
 *  @param {Object} a  八字四柱的拼音
 */
function getZizuo(a) {
  var h = {};
  h.nian = getDishiWithDizhi(a.nian.tiangan, a.nian.dizhi);
  h.yue = getDishiWithDizhi(a.yue.tiangan, a.yue.dizhi);
  h.ri = getDishiWithDizhi(a.ri.tiangan, a.ri.dizhi);
  h.shi = getDishiWithDizhi(a.shi.tiangan, a.shi.dizhi);
  return h
}
/**
 * 获取八字纳音信息
 *  @param {Object} a  八字四柱的拼音
 */
function getNayin(a) {
  var i = {}, h = getBaziChNames(a);
  return i.nian = BaziInfo.nayinName[Math.floor(BaziInfo.ganzhi.indexOf(h.nian.tiangan + h.nian.dizhi) / 2)],
    i.yue = BaziInfo.nayinName[Math.floor(BaziInfo.ganzhi.indexOf(h.yue.tiangan + h.yue.dizhi) / 2)],
    i.ri = BaziInfo.nayinName[Math.floor(BaziInfo.ganzhi.indexOf(h.ri.tiangan + h.ri.dizhi) / 2)],
    i.shi = BaziInfo.nayinName[Math.floor(BaziInfo.ganzhi.indexOf(h.shi.tiangan + h.shi.dizhi) / 2)],
    i;
}
/**
 * 获取八字藏干中文信息
 *  @param {Object} a  八字四柱的拼音
 */
function getCangganChNames(a) {
  var i = getCanggan(a), h = {};
  h.nian = "";
  for (var e in i.nian) {
    s = i.nian[e];
    h.nian += BaziInfo.tiangan[s].name;
  }
  h.yue = "";
  for (var e in i.yue) {
    s = i.yue[e];
    h.yue += BaziInfo.tiangan[s].name;
  }
  h.ri = "";
  for (var e in i.ri) {
    s = i.ri[e];
    h.ri += BaziInfo.tiangan[s].name;
  }
  h.shi = "";
  for (var e in i.shi) {
    var s = i.shi[e];
    h.shi += BaziInfo.tiangan[s].name;
  }
  return h;
}
/**
 * 获取八字藏干拼音信息
 *  @param {Object} a  八字四柱的拼音
 */
function getCanggan(a) {
  var i = {};
  return i.nian = BaziInfo.dizhi[a.nian.dizhi].canggan, i.yue = BaziInfo.dizhi[a.yue.dizhi].canggan,
    i.ri = BaziInfo.dizhi[a.ri.dizhi].canggan, i.shi = BaziInfo.dizhi[a.shi.dizhi].canggan,
    i;
}
/**
 * 获取神煞信息
 *  @param {Object} a  八字四柱的拼音
 */
function getShensha(a) {
  var i = {};
  return i.nian = getShenshaWithGanzhi(a, a.nian, "nian"), i.yue = getShenshaWithGanzhi(a, a.yue, "yue"),
    i.ri = getShenshaWithGanzhi(a, a.ri, "ri"), i.shi = getShenshaWithGanzhi(a, a.shi, "shi"),
    i;
}
/**
 * 根据干支获取神煞信息
 *  @param {Object} a  八字四柱的拼音
 *  @param {String} i  单个柱的拼音
 *  @param {String} h  四柱之一(年月日时)的拼音
 */
function getShenshaWithGanzhi(a, i, h) {
  var e = [];
  for (var s in BaziInfo.shensha) {
    var n = BaziInfo.shensha[s];
    // 2022-7-21:fix:业务需求去掉'太白星'
    if (n.result == '太白星') continue
    var c = checkShenshaRules(n, a, i, h);
    null != c && (e.push(c));
  }
  return e;
}
/**
 * 根据神煞规则获取对应神煞
 *  @param {Object} a  神煞信息
 *  @param {Object} i  八字四柱的拼音
 *  @param {String} h  当前单个柱的拼音
 *  @param {String} e  四柱类型(年月日时)的拼音
 */
function checkShenshaRules(a, i, h, e) {
  var s = !0;
  for (var n in a.base4zhus) if (i[a.base4zhus[n]][a.baseGanOrZhi] == a.baseGanZhiName) {
    s = !1;
    break;
  }
  if (s) return null;
  if ("all" != a.check4zhus && a.check4zhus.indexOf(e) < 0) return null;
  var c = "";
  return c = "zhu" == a.checkGanOrZhi ? h.tiangan + h.dizhi : h[a.checkGanOrZhi],
    a.checkGanZhiNames.indexOf(c) < 0 ? null : a.result;
}
/**
 * 获取八字胎元信息
 *  @param {Object} a  八字四柱的拼音
 */
function getTaiyuan(a) {
  var i = BaziInfo.tianganNames[(BaziInfo.tianganNames.indexOf(a.yue.tiangan) + 1) % 10], h = BaziInfo.dizhiNames[(BaziInfo.dizhiNames.indexOf(a.yue.dizhi) + 3) % 12];
  return BaziInfo.tiangan[i].name + BaziInfo.dizhi[h].name;
}
/**
 * 获取八字胎元信息
 *  @param {Object} a  八字四柱的拼音
 */
function getMinggong(a) {
  var h = getMinggongDizhi(a), e = (BaziInfo.tianganNames.indexOf(BaziInfo.tiangan[a.nian.tiangan].minggongTianganQi) + (h + 10)) % 10, s = BaziInfo.tianganNames[e], n = BaziInfo.minggongDizhiList[h];
  return BaziInfo.tiangan[s].name + BaziInfo.dizhi[n].name;
}
/**
 * 根据八字四柱拼音获取八字胎元信息
 *  @param {Object} a  八字四柱的拼音
 */
function getMinggongDizhi(a) {
  return ((26 - BaziInfo.minggongDizhiList.indexOf(a.shi.dizhi) - BaziInfo.minggongDizhiList.indexOf(a.yue.dizhi) - 2) % 12 + 11) % 12;
}
/**
 * 根据八字四柱拼音获取对冲生肖信息
 *  @param {Object} a  八字四柱的拼音
 */
function getDuichong(a) {
  var i = BaziInfo.dizhiNames.indexOf(a.ri.dizhi);
  return BaziInfo.shiershegnxiao[(i + 6) % 12];
}
/**
 * 根据八字四柱拼音获取三煞信息
 *  @param {Object} a  八字四柱的拼音
 */
function getSansha(a) {
  var i = BaziInfo.dizhiNames.indexOf(a.ri.dizhi);
  return BaziInfo.sansha[i % 4];
}
/**
 * 根据八字四柱拼音获取文昌位信息
 *  @param {Object} a  八字四柱的拼音
 */
function getWenchangwei(a) {
  return BaziInfo.tiangan[a.nian.tiangan].wenchangwei;
}
/**
 * 根据八字四柱拼音获取本命佛
 *  @param {Object} a  八字四柱的拼音
 */
function getBenmingfo(a) {
  var i = BaziInfo.shiershegnxiao.indexOf(a.Animal);
  return BaziInfo.benmingfo[i];
}
/**
 * 根据八字四柱拼音获取八字十神
 *  @param {Object} a  八字四柱的拼音
 *  @param {Number} r  性别 0 - 男, 1 - 女
 */
function getBaziShishen(a, r) {
  var i = {};
  return i.nian = get1TianganShishen(a.ri.tiangan, a.nian.tiangan), i.yue = get1TianganShishen(a.ri.tiangan, a.yue.tiangan),
    i.ri = getShiShenRiZhu(r), i.shi = get1TianganShishen(a.ri.tiangan, a.shi.tiangan),
    i;
}
/**
 * 根据四柱天干查十神
 *  @param {String} a  天干1
 *  @param {String} i  天干2
 */
function get1TianganShishen(a, i) {
  var h = BaziInfo.tiangan[a].wuxing, e = BaziInfo.tiangan[i].wuxing, s = BaziInfo.wuxingRelationship[e][h];
  return BaziInfo.shishenShengke[s][BaziInfo.tiangan[i].yinyang ^ BaziInfo.tiangan[a].yinyang ^ 1];
}
/**
 * 日柱十神
 *  @param {Number} a  性别 0 - 男, 1 - 女
 */
function getShiShenRiZhu(a) {
  return a == "0" ? "元男" : "元女";
}
/**
 * 根据八字四柱拼音获取藏干十神
 *  @param {Object} a  八字四柱的拼音
 */
function getCangganShishen(a) {
  var i = getCanggan(a), h = {};
  h.nian = [];
  for (var e in i.nian) {
    s = i.nian[e];
    h.nian.push(get1TianganShishen(a.ri.tiangan, s));
  }
  h.yue = [];
  for (var e in i.yue) {
    s = i.yue[e];
    h.yue.push(get1TianganShishen(a.ri.tiangan, s));
  }
  h.ri = [];
  for (var e in i.ri) {
    s = i.ri[e];
    h.ri.push(get1TianganShishen(a.ri.tiangan, s));
  }
  h.shi = [];
  for (var e in i.shi) {
    var s = i.shi[e];
    h.shi.push(get1TianganShishen(a.ri.tiangan, s));
  }
  return h;
}
/**
 * 获取十年大运
 *  @param {Object} a  八字四柱的拼音
 *  @param {Object} i  农历日期的当天详细信息
 *  @param {Number} e  小时
 *  @param {Number} s  分钟
 *  @param {Number} n  性别
 */
function getShiniandayun(a, i, e, s, n) {
  var c = n == BaziInfo.tiangan[a.nian.tiangan].yinyang;
  var u = getSolarTerms(i, e, s, c);
  console.log(u);
  var r = BaziInfo.tiangan[a.yue.tiangan].name + BaziInfo.dizhi[a.yue.dizhi].name, y = BaziInfo.ganzhi.indexOf(r), z = {};
  z.dayun = [];
  for (var G = 0; G < 8; G++) {
    var Z = {};
    Z.dayun = BaziInfo.ganzhi[(60 + y - Math.pow(-1, c) * (G + 1)) % 60];
    // b:大运天干 j:大运地支
    var b = "", j = "";
    for (var g in BaziInfo.tiangan) if (BaziInfo.tiangan[g].name == Z.dayun[0]) {
      b = g;
      break;
    }
    for (var g in BaziInfo.dizhi) if (BaziInfo.dizhi[g].name == Z.dayun[1]) {
      j = g;
      break;
    }
    // 添加大运的字符串分割
    Z.dayunArr = [...Z.dayun];
    Z.shishen = get1TianganShishen(a.ri.tiangan, b);
    Z.dishi = getDishiWithDizhi(a.ri.tiangan, j);
    Z.niansui = 10 * G + parseInt(u.years) + 1;
    // bugfix:需要月份,天数+起运月份,天数是否超过一年
    let addMonth = false;
    let addYear = false;
    if (u.days + i.cDay > 30) addMonth = true;
    if ((u.months + i.cMonth + (addMonth ? 1 : 0)) > 12) addYear = true;
    if (addYear) {
      Z.niansui += 1;
    }
    Z.nianfen = parseInt(i.cYear) + Z.niansui - 1;
    // 添加大运的藏干十神;b:大运天干 j:大运地支
    // 获取大运地支藏干
    let dayunCanggan = BaziInfo.dizhi[j].canggan;
    Z.cangganshishen = get1TianganShishen(a.ri.tiangan, dayunCanggan[0]);
    z.dayun.push(Z);
  }
  var k = Calendar.dateWithYMD(i, u.years, u.months, u.days);
  z.explain = "起大运周岁：" + u.years + "岁" + u.months + "个月" + u.days + "天，每一交大运年" + k.cMonth + "月" + k.cDay + "日起运(公历)。";
  return z;
}
/**
 * 十年大运正逆排时间差计算
 *  @param {Object} a  农历日期的当天详细信息
 *  @param {Number} i  小时
 *  @param {Number} e  分钟
 *  @param {Number} s  阴阳0,1
 */
function getSolarTerms(a, i, e, s) {
  var n = BaziInfo.ershisiqiTimes[a.cYear], c = {};
  if (s) {
    // 顺排
    for (Z = 0; Z < 12; Z++) {
      var G = n[Z][1][2].split(":")
      if (parseInt(n[Z][1][0]) > parseInt(a.cMonth) || (parseInt(n[Z][1][0]) == parseInt(a.cMonth) && parseInt(n[Z][1][1]) > parseInt(a.cDay)) || (parseInt(n[Z][1][0]) == parseInt(a.cMonth) && parseInt(n[Z][1][1]) == parseInt(a.cDay) && parseInt(G[0]) >= parseInt(i))) {
        z = Calendar.solar2lunar(a.cYear, n[Z][1][0], n[Z][1][1]);
        break;
      }
      z = Calendar.solar2lunar(a.cYear + 1, BaziInfo.ershisiqiTimes[a.cYear + 1][0][1][0], BaziInfo.ershisiqiTimes[a.cYear + 1][0][1][1]),
        G = BaziInfo.ershisiqiTimes[a.cYear + 1][0][1][2].split(":");
    }
    // 分钟差
    var u = parseInt(G[1]) - e;
    // 小时差
    var r = parseInt(G[0]) - i;
    // 计算两个日期的天数差
    var y = Calendar.deltaBetweenTwoDays(a, z);
    // 3天1年,1天4个月,1小时5天
    return u < 0 && (u += 60, r--), r < 0 && (r += 12, y--),
      console.log("deltaDays: " + y + " deltaHours:" + r), c.years = Math.floor(y / 3),
      c.months = y % 3 * 4 + Math.floor(r / 6), c.days = r % 6 * 5 + Math.floor(u / 12),
      c;
  }
  // 逆排
  for (var z, G, Z = 11; Z >= 0; Z--) {
    var G = n[Z][1][2].split(":");
    if (parseInt(n[Z][1][0]) < parseInt(a.cMonth) || (parseInt(n[Z][1][0]) == parseInt(a.cMonth) && parseInt(n[Z][1][1]) < parseInt(a.cDay)) || (parseInt(n[Z][1][0]) == parseInt(a.cMonth) && parseInt(n[Z][1][1]) == parseInt(a.cDay) && parseInt(G[0]) < parseInt(i))) {
      z = Calendar.solar2lunar(a.cYear, n[Z][1][0], n[Z][1][1]);
      break;
    }
    z = Calendar.solar2lunar(a.cYear - 1, BaziInfo.ershisiqiTimes[a.cYear - 1][11][1][0], BaziInfo.ershisiqiTimes[a.cYear - 1][11][1][1]),
      G = BaziInfo.ershisiqiTimes[a.cYear - 1][11][1][2].split(":");
  }
  // 分钟差
  var u = e - parseInt(G[1]);
  // 小时差
  var r = i - parseInt(G[0]);
  // 计算两个日期的天数差
  var y = Calendar.deltaBetweenTwoDays(z, a);
  return u < 0 && (u += 60, r--), r < 0 && (r += 24, y--), console.log("**************"),
    console.log("deltaDays: " + y + " deltaHours:" + r + "deltaMinutes:" + u), c.years = Math.floor(y / 3),
    c.months = y % 3 * 4 + Math.floor(r / 6), c.days = r % 6 * 5 + Math.floor(u / 12),
    console.log(c), console.log(u), c;
}
/**
 * 获取空亡
 *  @param {Object} a  八字四柱的拼音
 */
function getKongwang(a) {
  let nianzhu = a.nian.tiangan + a.nian.dizhi;
  let yuezhu = a.yue.tiangan + a.yue.dizhi;
  let rizhu = a.ri.tiangan + a.ri.dizhi;
  let shizhu = a.shi.tiangan + a.shi.dizhi;
  let sizhu = [nianzhu, yuezhu, rizhu, shizhu];
  let result = [];
  for (let ele of sizhu) {
    for (let item of Object.keys(BaziInfo.kongwangList)) {
      if (BaziInfo.kongwangList[item].list.includes(ele)) {
        result.push(BaziInfo.kongwangList[item].name);
      }
    }
  }
  return result
}
/**
 * 神煞中判断并添加空亡
 *  @param {Object} baziChName  八字四柱中文
 *  @param {Object} kongwang  四柱的空亡属性
 *  @param {Object} shensha  四柱的神煞
 */
function addShenshaKongwang(baziChName, kongwang, shensha) {
  // 依据年柱 和 日柱的空亡,分别看其他三柱的地支,若其他三柱的地支中包含其空亡属性中一值,则神煞中有空
  let nianKongwang = kongwang[0];
  let riKongwang = kongwang[2];

  let nianzhu = baziChName.nian.dizhi;
  let yuezhu = baziChName.yue.dizhi;
  let rizhu = baziChName.ri.dizhi;
  let shizhu = baziChName.shi.dizhi;
  // 年十神是否有空亡
  if (riKongwang.includes(nianzhu)) {
    shensha.nian.push("空亡");
  }
  // 月十神
  if (riKongwang.includes(yuezhu) || nianKongwang.includes(yuezhu)) {
    shensha.yue.push("空亡");
  }
  // 日十神
  if (nianKongwang.includes(rizhu)) {
    shensha.ri.push("空亡");
  }
  // 时十神
  if (riKongwang.includes(shizhu) || nianKongwang.includes(shizhu)) {
    shensha.shi.push("空亡");
  }
}
/**
 * 获取福神财神喜神贵人方位
 *  @param {Object} a  八字四柱的拼音
 */
function getFucaixigui(a) {
  let arr = [];
  arr.push(BaziInfo.tiangan[a.ri.tiangan].caishen);
  arr.push(BaziInfo.tiangan[a.ri.tiangan].fushen);
  arr.push(BaziInfo.tiangan[a.ri.tiangan].xishen);
  arr.push(BaziInfo.tiangan[a.ri.tiangan].nanguiren);
  arr.push(BaziInfo.tiangan[a.ri.tiangan].nvguiren);
  return arr
}
/**
 * 八字财库计算方法
 *  @param {Object} param 八字信息
 *  @eg ganzhi: '',  // 干支
 *      dizhi: ''   // 日元
 *  @return {Object} 返回计算后的信息
 */
export function caikuCalc(ganzhi,riyuan) {
  // 日元与其他各柱干支比较
  // 财: 甲,乙-土;丙丁-金;戊己-水;庚辛-木;壬癸-火
  // 财库: 甲,乙-戌;丙丁-丑;戊己-辰;庚辛-未;壬癸-戌
  let caikuList = {
    "甲": {
      cai: ['戊', '己', '辰', '戌', '丑', '未'],
      ku: ['戌']
    },
    "乙": {
      cai: ['戊', '己', '辰', '戌', '丑', '未'],
      ku: ['戌']
    },
    "丙": {
      cai: ['庚', '辛', '申', '酉'],
      ku: ['丑']
    },
    "丁": {
      cai: ['庚', '辛', '申', '酉'],
      ku: ['丑']
    },
    "戊": {
      cai: ['壬', '癸', '子', '亥'],
      ku: ['辰']
    },
    "己": {
      cai: ['壬', '癸', '子', '亥'],
      ku: ['辰']
    },
    "庚": {
      cai: ['甲', '乙', '寅', '卯'],
      ku: ['未']
    },
    "辛": {
      cai: ['甲', '乙', '寅', '卯'],
      ku: ['未']
    },
    "壬": {
      cai: ['丙', '丁', '巳', '午'],
      ku: ['戌']
    },
    "癸": {
      cai: ['丙', '丁', '巳', '午'],
      ku: ['戌']
    }
  };
  let isCaixing =  caikuList[riyuan] ? caikuList[riyuan].cai.includes(ganzhi) : false;
  let isCaiku =  caikuList[riyuan] ? caikuList[riyuan].ku.includes(ganzhi) : false;
  return {
    isCaixing,
    isCaiku
  }
}